package com.example.spingbootvue.mapper;

import com.example.spingbootvue.entity.UseraCommunity;
import org.apache.ibatis.annotations.*;
import com.example.spingbootvue.entity.User;

import java.time.LocalDate;
import java.util.List;

@Mapper
public interface UserCommunityMapper {

    //查询所有用户发布的动态信息
    @Select("SELECT * FROM community")
    List<User> getUser();

    //查询当前用户发布的动态信息
    @Select("SELECT * FROM community WHERE sickname=#{sickname}")
    List<User> getUserBySickname(String sickname);

    //查询用户发布的动态别人评论的信息
    @Select("SELECT * FROM comment_msg WHERE foreignid=#{foreignid}")
    List<UseraCommunity> getUserComment(Integer foreignid);

    //插入发布的动态的信息
    @Insert("INSERT INTO community(account,sickname,content,image,posttime,commentimage) VALUES (#{account},#{sickname},#{content},#{image},#{posttime},#{commentimage})")
    int insertDtMsg(String account, String sickname, String content, String image, LocalDate posttime,String commentimage);

    //插入发布的评论的信息
    @Insert("INSERT INTO comment_msg(foreignid,sickname,image,replycontent,replytime,replysickname,account)" +
            " VALUES (#{foreignid},#{sickname},#{image},#{replycontent},#{replytime},#{replysickname},#{account})")
    int insertPlMsg(Integer foreignid, String sickname, String image, String replycontent, LocalDate replytime, String replysickname,String account);

    //将发布动态信息的pid与评论信息中pid进行绑定
    @Update("UPDATE community SET pid=#{pid} WHERE foreignid=#{foreignid}")
    int updatePid(Integer foreignid, Integer pid);

    //查询此条动态有几条评论
    @Select("SELECT  COUNT(*) FROM comment_msg WHERE foreignid = #{foreignid}")
    int countComment(Integer foreignid);

    //动态点赞,点击点赞按钮点赞数量加一
    @Update("UPDATE community SET likenum = likenum+1 WHERE foreignid = #{foreignid}")
    int addLikenum(Integer foreignid);

    //动态点赞,当用户已经点赞,再次点击点赞按钮点赞数量减一
    @Update("UPDATE community SET likenum = likenum-1 WHERE foreignid = #{foreignid}")
    int minusLikenum(Integer foreignid);

    //用户点击删除可以将自己发布的动态删除
    @Delete("DELETE FROM community WHERE foreignid = #{foreignid}")
    int deleteDtMsg(Integer foreignid);

    //插入回复其他用户的评论
    @Insert("INSERT INTO comment_msg(foreignid,sickname,image,replycontent,replytime,pid, replysickname,account)" +
            " VALUES (#{foreignid},#{sickname},#{image},#{replycontent},#{replytime},#{id},#{ replysickname},#{account}) ")
    int ReplyComment(Integer foreignid, String sickname, String image, String replycontent, LocalDate replytime,Integer id,String replysickname,String account);

    //获取被回复的用户昵称
//    @Select(" SELECT sickname FROM `comment_msg` WHERE id IN (SELECT pid FROM `comment_msg` WHERE pid IS NOT NULL AND foreignid=#{foreignid}) ")
//    List<UseraCommunity> getSickname(Integer foreignid);

   //用户在搜索框中输入关键字进行搜索
   @Select("SELECT * FROM `community` WHERE content LIKE CONCAT('%', #{content}, '%')")
   List<User> getUserByContentLike(String content);

   //个人主页编辑用户输入昵称可以更新
   @Update("UPDATE user SET sickname = #{sickname} WHERE account = #{account}")
   int updatesickname1(String sickname, String account);
   @Update("UPDATE community SET sickname = #{sickname} WHERE account = #{account}")
   int updatesickname2(String sickname, String account);
   @Update("UPDATE comment_msg SET sickname = #{sickname} WHERE account = #{account}")
   int updatesickname3(String sickname, String account);
   @Update("UPDATE comment_msg AS t1 JOIN comment_msg AS t2 ON t1.pid = t2.id SET t1.replysickname = t2.sickname")
   int updatesickname4();


}
